#include "MainHeader.h"
/*class cClipboard{
public: 
	cClipboard();
	~cClipboard();
	u8 copy;
    sCombos cboCurClip;
	u16 thisclipboard[16][256][4];
    Image  cb[16];
    HWND me;
	HWND cb;
	int Create();
	int Erase();
    int inuse;
}
BOOL CALLBACK	    ClipBoardProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK    CBImageProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
*/

BOOL CALLBACK ClipBoardProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){



	switch (message) 
	{
	case WM_INITDIALOG:
	ClipBoard.me= hWnd;
	ClipBoard.Create();
		break;
		
		
	case WM_LBUTTONDOWN:
		
		
		break;
	case WM_COMMAND:
		switch(LOWORD(wParam)){
		
		
			case WM_DESTROY:
				ShowWindow(hWnd,SW_HIDE);
				break;
			case cboCBNo:
                if(HIWORD(wParam)==CBN_SELCHANGE){
                   ClipBoard.curclip= ClipBoard.cboCurClip.GetListIndex();
				   InvalidateRect(ClipBoard.cb,0,1);
				  //ClipBoard.CheckforBlanks(N);
				}
				break;
			case cmdErase:
				 ClipBoard.Erase(ClipBoard.curclip,0xFFFF);

				break;
		}
			
	
	break;
		
	case WM_VSCROLL:	// exact same idea, but V scroll instead of H scroll
		
		break;
	case WM_PAINT:
	
		break;
		case WM_SHOWWINDOW:
		 // LoadCurSprite();	
			break;
			
			
	}
	
//return DefWindowProc(hWnd, message, wParam, lParam);
	return 0;
}
cClipboard::cClipboard(){
}
cClipboard::~cClipboard(){

}


int cClipboard::Create(){
	char blah[256]={0};
	int i =0;
	cboCurClip.Init(GetDlgItem(me,cboCBNo));
    for(i=0;i<16;i++){
		sprintf(blah,"%d",i);
		cboCurClip.Additem(blah);
        cbi[i].Create(256,256);
	}
	cboCurClip.SetListIndex(0);

	CreatePic();
    Erase();
	return 0;
}

int cClipboard::Erase(){
	int i =0;

	for(i =0;i<16;i++)
         Erase(i,0xFFFF);
    

return 0;
}



int cClipboard::CheckforBlanks(HDC hdc){
int i=0,x=0,y=0;
  for(i=0;i<256;i++){

	if( (signed)(thisclipboard[curclip][i][0]==0xFFFF)
	  &&(thisclipboard[curclip][i][1]==0xFFFF)
	  &&(thisclipboard[curclip][i][2]==0xFFFF)
	  &&(thisclipboard[curclip][i][3]==0xFFFF)){
		//if we are here then we draw the X
	//	for(x=0;x<16;x++){
			for(y=0;y<16;y++){
				 SetPixel(hdc, ((i%16)*16)+(y),   ((i/16)*16)+(y), RGB(255,0,0));
				 SetPixel(hdc, ((i%16)*16)+(16-y),((i/16)*16)+(y), RGB(255,0,0));
			}
	//	}




	}
  }

	return 0;
}

LRESULT CALLBACK CBImageProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	HDC hdc;
	PAINTSTRUCT ps;
    char buffer[256]={0};
	switch (message) 
	{
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		
			ClipBoard.cbi[ClipBoard.curclip].Blit(hdc,0,0,256,256,0,0);
			ClipBoard.CheckforBlanks(hdc);
		// SceneryPic.Blit(hdc, 0,0,ThisBackground->X*8,ThisBackground->Y*8,	nHScroll[SceneHScroll]*8,nVScroll[12]*8);
		EndPaint(hWnd, &ps);
		break;
		
	case WM_LBUTTONDOWN:
	    SetTiles(lParam);
		break; 
	case WM_MOUSEMOVE:
		sprintf(buffer,"Tile info: %d",(GetX(lParam)/16)+(GetY(lParam)/16)*16);
		SetWindowText(GetDlgItem(ClipBoard.me,fraInfo),buffer);
		sprintf(buffer,"Foreground: %X",ClipBoard.thisclipboard[ClipBoard.curclip][(GetX(lParam)/16)+((GetY(lParam)/16)*16)][0]);
		SetWindowText(GetDlgItem(ClipBoard.me,lblForeground),buffer);
		sprintf(buffer,"Level: %X",ClipBoard.thisclipboard[ClipBoard.curclip][(GetX(lParam)/16)+((GetY(lParam)/16)*16)][1]);
		SetWindowText(GetDlgItem(ClipBoard.me,lblLevel),buffer);
		sprintf(buffer,"BackLayer: %X",ClipBoard.thisclipboard[ClipBoard.curclip][(GetX(lParam)/16)+((GetY(lParam)/16)*16)][2]);
		SetWindowText(GetDlgItem(ClipBoard.me,lblBackLayer),buffer);
		sprintf(buffer,"Clipdata: %X",ClipBoard.thisclipboard[ClipBoard.curclip][(GetX(lParam)/16)+((GetY(lParam)/16)*16)][3]);
		SetWindowText(GetDlgItem(ClipBoard.me,lblClipdata),buffer);
		break;
		
		
		
	}
	
	return DefWindowProc(hWnd, message, wParam, lParam);
	//return 0;
}

int cClipboard::CreatePic(){


	// Fill in the window class structure with parameters 
	// that describe the main window. 
	int myerror=0;
	WNDCLASSEX penis6;
	memset(&penis6,0,sizeof(penis6));
	penis6.cbSize = sizeof(penis6);		   // size of structure 
	penis6.style = CS_HREDRAW | 
		CS_VREDRAW; 				   // redraw if size changes 
	penis6.lpfnWndProc =CBImageProc;	  // points to window procedure 
	penis6.cbClsExtra = NULL;				// no extra class memory 
	penis6.cbWndExtra =NULL; 			   // no extra window memory 
	penis6.hInstance = hGlobal;		   // handle to instance 
	penis6.hIcon = NULL; 			 // predefined app. icon 
	penis6.hCursor = NULL;				// predefined arrow 
	//	penis6.hbrBackground = (HBRUSH)GetStockObject(15);				   // white background brush 
	penis6.lpszMenuName =  NULL;    // name of menu resource 
	penis6.lpszClassName = "cBalls";  // name of window class 
	
	
	// Register the window class. 
	
	RegisterClassEx(&penis6); 
	myerror =GetLastError();
    cb= CreateWindowEx(NULL,"cBalls",NULL, WS_CHILD|WS_VISIBLE ,22,57,256,256,me,0,hGlobal,0);
	return 0;
}



int cClipboard::DrawSlots(){
//Determine layer to draw
	int base[3]={0};
	int what[3]={0};
	int i =0;
	int j =0;
	int thisX=0, thisY=0;
	cbi[curclip].Clear();
	cbi[curclip].SetPalette(GameEngine->Members.PcPalMem);
	for(j=0;j<256;j++){
		for(i=0;i<3;i++) base[i]=thisclipboard[curclip][j][i];
//0=2
		for(i =2;i>=0; i--){
			if(base[i]==0xFFFF) continue;
			thisX=((j%16));
			thisY=((j/16));
			//cbi[curclip].Draw(TileImage,((j%16)*16), ((j/16)) * 16, TSA.nTSA[(base[i])*4]);
		//	cbi[curclip].Draw(TileImage,((j%16)*16) + 8, ((j/16)) * 16, TSA.nTSA[((base[i])*4)+1]);
			//cbi[curclip].Draw(TileImage,((j%16)*16), ((j/16) * 16)+8, TSA.nTSA[((base[i])*4)+2]);
			//cbi[curclip].Draw(TileImage,((j%16)*16)+8, ((j/16) * 16)+8, TSA.nTSA[((base[i])*4)+3]);
		 //TILE =(Map->TileBuf2D[thisX+(thisY * (Map->X))]);
			BitBlt(cbi[curclip].DC(),(thisX)* 16, (thisY)* 16,16,16,img[iTileset].DC(),((base[i])%16)*16,((base[i])/16)*16,SRCCOPY);
					//	img[iTileset].Blit(hdc, 0,0,coord.right - coord.left ,coord.bottom-coord.top,0,nVScroll[sTileset]*16);
			
		
		}
	}
	InvalidateRect(cb,0,1);
	return 0;
}


int cClipboard::CopyTiles(LPARAM lParam, int surface){//Surface is where to place them 0 for room, 1 for ya know ;)
      //Just do it in order
    int baseX=(GetX(lParam)/16)+nHScroll[sHMap];
	int baseY=(GetY(lParam)/16)+nVScroll[sVMap];
	int thisX=0, thisY=0;
    //Since whe have the base this should be rather easy.
	int i=0;//Tile counter
	
	for(i = 0; i <256;i++){//256 tiles per clipboard
		thisX=(i%16);
		thisY=(i/16);
		if(thisclipboard[curclip][i][0]!=0xFFFF){
				ThisForeground->TileBuf2D[(baseX+thisX) + (baseY + thisY)* ThisForeground->X]=thisclipboard[curclip][i][0];
			    ThisForeground->Dirty = 1;
		}
		if(thisclipboard[curclip][i][1]!=0xFFFF){
				ThisLevelData->TileBuf2D[(baseX+thisX) + (baseY + thisY)* ThisLevelData->X]=thisclipboard[curclip][i][1];
			    ThisLevelData->Dirty  = 1;
		}	 
		if(thisclipboard[curclip][i][2]!=0xFFFF){
				ThisBackLayer->TileBuf2D[(baseX+thisX) + (baseY + thisY) * ThisBackLayer->X]=thisclipboard[curclip][i][2];
		        ThisBackLayer->Dirty = 1;
		}
				
		if(thisclipboard[curclip][i][3]!=0xFFFF)
				 ThisClipData->TileBuf2D[(baseX+thisX) + (baseY + thisY) * ThisClipData->X]=thisclipboard[curclip][i][3];
			 
  
	}
   
	return 0;
}


int cClipboard::Erase(int board,int slot){
int i =0;

if(slot == 0xFFFF){
memset(&thisclipboard[board],0xFFFF,2048);
cbi[board].Clear();
cbi[board].SetPalette(GameEngine->Members.PcPalMem);
}else{
memset(&thisclipboard[board][slot],0xFFFF,4);
cbi[board].Clear();
cbi[board].SetPalette(GameEngine->Members.PcPalMem);
}
InvalidateRect(cb,0,1);

	return 0;
}



int SetTiles(LPARAM lParam){//Layer to use, XY value, if it's bigger then 16x16
int baseX=((GetX(lParam)/16));
int baseY=((GetY(lParam)/16)) ;
int y=0;
int x=0;
int ny=0;
int nx=0;
int i=0;
nRoomBuff* Layer;
int width=0; 
if(!MPToUse){//Map
	for(i =0;i<4;i++){
		if(i == 0){
			if(CheckBoxes[Foreground].value()){
				Layer=ThisForeground;
			}else{
				continue;
			}
		}else if(i==1){
			if(CheckBoxes[Level].value()){
				Layer=ThisLevelData;
			}else{
				continue;
			}

		}else if(i == 2){
			if(CheckBoxes[Background].value()){
				Layer=ThisBackLayer;
			}else{
				continue;
			}
		}else if(i==3){
			if(CheckBoxes[Clip].value()){
				Layer=ThisClipData;
			}else{
				continue;
			}

		}else{
				break;
		}
			width=Layer->X;

	  for(y=0;y<mpMap.Height;y++){
			for(x=0;x<mpMap.Width;x++){
				if((baseX +x) +  (baseY + y)* (16)>=256) continue; 
				ClipBoard.thisclipboard[ClipBoard.curclip][(baseX +x) +  (baseY + y)* (16)][i] = Layer->TileBuf2D[Tile +x + (y * width) ];	  
			    ClipBoard.DrawSlots();
			}
	  }
	}
}else if(MPToUse){//Tileset 
		if(CheckBoxes[Foreground].value()) i = 0;
		if(CheckBoxes[Level].value())      i = 1;
		if(CheckBoxes[Background].value()) i = 2;
		if(CheckBoxes[Clip].value())       i = 3;
 for(y=0;y<mpTileset.Height;y++){
		for(x=0;x<mpTileset.Width;x++){
				ClipBoard.thisclipboard[ClipBoard.curclip][(baseX +x) +  (baseY + y)* (16)][i]= Tile +x + (y * 16);	  
		}
	}
 ClipBoard.DrawSlots();
}
 



return 0; 
}


